package com.hy.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.common.Result;
import com.hy.entity.Teacher;
import com.hy.entity.TeacherLike;
import com.hy.mapper.TeacherMapper;
import com.hy.service.TeacherLikeService;
import com.hy.service.TeacherService;
import com.hy.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;

@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired
    private UserUtil userUtil;

    @Autowired
    private TeacherLikeService teacherLikeService;

    @Override
    public List<Teacher> getAllTeachers() {
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teacher::getDeleteFlag, 0)
               .orderByDesc(Teacher::getCreateTime);
        return list(wrapper);
    }

    @Override
    public Teacher getTeacherById(Long id) {
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teacher::getId, id)
               .eq(Teacher::getDeleteFlag, 0);
        return getOne(wrapper);
    }

    @Override
    @Transactional
    public boolean addTeacher(Teacher teacher) {
        teacher.setDeleteFlag(0);
        teacher.setTeacherLikeNumbers(0L);
        teacher.setCreateTime(new Date());
        teacher.setUpdateTime(new Date());
        return save(teacher);
    }

    @Override
    @Transactional
    public boolean updateTeacher(Teacher teacher) {
        teacher.setUpdateTime(new Date());
        return updateById(teacher);
    }

    @Override
    @Transactional
    public boolean deleteTeacher(Long id) {
        LambdaUpdateWrapper<Teacher> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Teacher::getId, id)
                    .set(Teacher::getDeleteFlag, 1)
                    .set(Teacher::getUpdateTime, new Date());
        return update(updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Void> likeTeacher(Teacher teacher) {

        // 判断id是否为空
        if(ObjectUtil.isEmpty(teacher.getId())){
            return Result.error("入参为空");
        }

        Integer userId = userUtil.getUserIdFromToken();

        // 查询是否点赞过
        boolean liked = teacherLikeService.isLiked(userId, teacher.getId());
        if(liked){
            return Result.error("你已经点赞过啦");
        }

        // 点赞+1
        Teacher dbTeacher = getById(teacher.getId());
        if (dbTeacher != null) {

            Date date = new Date();
            long likeNum = dbTeacher.getTeacherLikeNumbers() == null ? 0L : dbTeacher.getTeacherLikeNumbers();
            dbTeacher.setTeacherLikeNumbers(likeNum + 1);
            dbTeacher.setUpdateTime(date);
            updateById(dbTeacher);

            // 保存老师用户点赞数据
            TeacherLike teacherLike = new TeacherLike();
            teacherLike.setTeacherId(teacher.getId());
            teacherLike.setUserId(userId);
            teacherLike.setDeleteFlag(0);
            teacherLike.setCreateTime(date);
            teacherLike.setUpdateTime(date);
            teacherLikeService.save(teacherLike);
        }
        return Result.success("点赞成功");
    }

    @Override
    public Teacher getLessonTeacherName(Integer teacherId) {
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teacher::getId, teacherId);
        return getOne(wrapper);
    }

    @Override
    public List<Teacher> getTeachersByLessonId() {
        return this.list();
    }
}
